/*
 * @Author: yuki
 * @Date: 2025-11-06 16:03:55
 * @LastEditTime: 2025-11-06 16:09:40
 * @Description: 用户管道
 */
import type { PipeTransform, ArgumentMetadata } from '@nestjs/common';
import { BadRequestException } from '@nestjs/common';

import type { GetUserInfoDto } from '@/user/dto/user.dto';

import { handleError } from './common';

export class GetUserPipe implements PipeTransform {
  async transform(
    value: Partial<GetUserInfoDto> | undefined,
    metadata: ArgumentMetadata
  ) {
    if (!value || (!value.id && !value.username)) {
      throw new BadRequestException('必须提供用户ID或用户名');
    }
    await handleError(metadata, value);
    return value;
  }
}
